// pages/mall/mall.js

var app = getApp();
var page=1;
var selectedNews = false;
var newsList = [];

Page({
  data:{
    mall_picture: '',
    mall_url: '',
    selected: true,
    goodsList:[],
    isowner:false
  },
  //点击banner图进入链接页面
  showUlr: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: '../outurl/outurl?url=' + url,
    })
  },
  //点击进入名片
  showCarte: function(e) {
    wx.switchTab({
      url: '/pages/carteShow/carteShow'
    })
  },
  //点击进入详情页面
  bindGoodsTap: function (e) {
    var id = e.detail.button_typId;
    var title = e.detail.button_status;
    wx.navigateTo({
      url: '../goodsDetail/goodsDetail?id=' + id + '&title=' + title,
    })
  },
  //点击购物车图标进入购物车页面
  bindBuyCarTap: function (e) {
    wx.switchTab({
      url: '../buyCar/buyCar?uid=' + app.globalData.userInfo.id
    })
  },
  //获取商品列表
  getGoodsList: function () {
    var that = this;
    var url2 = '/shop_api/index';
    var params2 = {
      uid: app.globalData.SwitchTabParams.v_id,
      pagesize: 20,
      pageno: page
    };
    app.HttpPostSend(url2, params2, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
        selectedNews = false;
        that.setData({
          goodsList: []
        })
      } else {
        var goodsList = res.data.lists;
        for (let i = 0; i < goodsList.length; i++) {
          newsList.push(goodsList[i]);
        }
        if (goodsList.length < 20) {
          selectedNews = false;
        } else {
          selectedNews = true;
          page += 1;
        }
        that.setData({
          goodsList: newsList
        })
        wx.hideLoading();
      }
    });
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    if (options.mid != undefined && options.mid != 'undefined' ){
      app.globalData.SwitchTabParams.v_id = options.mid;
    }
    if (options.title != undefined && options.title != 'undefined') {
      wx.setNavigationBarTitle({
        title: options.title
      })
    }
    console.log(options);
    console.log(app.globalData.SwitchTabParams.v_id);
    var that = this;
    var url = '/shop/banner';
    var params = {
      uid: app.globalData.SwitchTabParams.v_id
    };
    app.HttpGetSend(url, params, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
      } else {
        if (res.data) {
          that.setData({
            mall_picture: res.data[0].img,
            mall_url: res.data[0].url,
          })
        }
      }
    });
    page = 1;
    selectedNews = false;
    newsList = [];
    that.getGoodsList();
  },
  // 点击进去咨询页面
  bindGoSocksTap: function (e) {
    wx.navigateTo({
      url: '../socks/socks',
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    if (app.globalData.userInfo.id != app.globalData.SwitchTabParams.v_id) {

      // 雷达添加查看记录 -查看商城数
      app.radarAdd(app.globalData.SwitchTabParams.v_id, 4, '', '', app.globalData.userInfo.logo)

      // 发送模板消息-访客到访通知
      app.sendTemplateMessage(app.globalData.SwitchTabParams.v_id, 1, '/radarPages/radarCenter/radarCenter?coming_status=1', '正在访问个人商城', '', '', '')
      this.setData({
        isowner: true
      })
    }else{
      this.setData({
        isowner:false
      })
    }

  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  // onShareAppMessage: function () {
  //   return {
  //     title: '万商',
  //     desc: '你要的名片',
  //     path: '/pages/home/home'
  //   }
  // },
  /** 
   * 页面上拉触底事件的处理函数 
   */
  onReachBottom: function () {
    if (selectedNews == true){
      var that = this;
      // 显示加载图标  
      wx.showLoading({
        title: '玩命加载中',
      })
      // 页数+1  
      that.getGoodsList();

    }else{
      wx.hideLoading();
    }
  }
})